package link.tengfei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import link.tengfei.bean.AdminUser;
import link.tengfei.util.ConnectionFactory;

public class AdminListDaoImpl implements AdminListDao {

	@Override
	public int findNum() {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		int total = 0;
		try {
			String sql = "select count(id) as total from admin_user where username is not null and name is not null and pass_id !=1";
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				total = rs.getInt("total");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return total;
	}

	@Override
	public List<AdminUser> findByPage(int page) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from admin_user where username is not null and name is not null and pass_id!=1 limit ?,10";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, page);
			rs = psmt.executeQuery();
			List<AdminUser> users = new ArrayList<>();
			while (rs.next()) {
				AdminUser user = new AdminUser();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getInt("sex"));
				user.setAge(rs.getString("age"));
				user.setUsername(rs.getString("username"));
				user.setEmail(rs.getString("email"));
				user.setState(rs.getInt("state"));
				users.add(user);
			}
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

	@Override
	public List<AdminUser> findByName(int page, String name) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from admin_user where username is not null and name is not null and pass_id!=1 and name=? limit ?,10";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, name);
			psmt.setInt(2, page);
			rs = psmt.executeQuery();
			List<AdminUser> users = new ArrayList<>();
			while (rs.next()) {
				AdminUser user = new AdminUser();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getInt("sex"));
				user.setAge(rs.getString("age"));
				user.setUsername(rs.getString("username"));
				user.setEmail(rs.getString("email"));
				user.setState(rs.getInt("state"));
				users.add(user);
			}
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

	@Override
	public int findNumByName(String name) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		int total = 0;
		String sql = "select count(id) as total from admin_user where username is not null and name is not null and name=? and pass_id!=1";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, name);
			rs = psmt.executeQuery();
			while (rs.next()) {
				total = rs.getInt("total");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return total;
	}

	@Override
	public int resetPwdById(int id) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int total = 0;
		String sql = "update admin_user set password=? where id=?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, "123456");
			psmt.setInt(2, id);
			total = psmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return total;
	}

	@Override
	public int delete(int id, int state) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int total = 0;
		String sql = "update admin_user set state=?, delete_date=? where id=?";
		try {
			psmt = conn.prepareStatement(sql);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			String deletedate = df.format(new Date());
			psmt.setInt(1, state);
			psmt.setString(2, deletedate);
			psmt.setInt(3, id);
			total = psmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return total;
	}

	@Override
	public int getStateById(int id) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		int state = 0;
		try {
			String sql = "select state from admin_user where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, id);
			rs = psmt.executeQuery();
			while (rs.next()) {
				state = rs.getInt("state");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return state;
	}

	@Override
	public AdminUser findUserById(int id) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from admin_user where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, id);
			rs = psmt.executeQuery();
			AdminUser user = new AdminUser();
			while (rs.next()) {
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getInt("sex"));
				user.setAge(rs.getString("age"));
				user.setPassid(rs.getInt("pass_id"));
				user.setUsername(rs.getString("username"));
				user.setEmail(rs.getString("email"));
				user.setAdddate(rs.getString("add_date"));
				user.setDeletedate(rs.getString("delete_date"));
				user.setBindingdate(rs.getString("binding_date"));
				user.setState(rs.getInt("state"));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

}
